package com.woniuxy.house.service.serviceimpl;
/**
 *
 * 还有距离最近的条件没完成
 *
 * sssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssssssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 * ssssssssssssssssss
 *
 * */
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.house.entity.HouseDetail;
import com.woniuxy.house.entity.HousePictures;
import com.woniuxy.house.entity.PageBean;
import com.woniuxy.house.entity.Post;
import com.woniuxy.house.entity.Source;
import com.woniuxy.house.mapper.HouseDetailMapper;
import com.woniuxy.house.mapper.HousePicturesMapper;
import com.woniuxy.house.mapper.SourceMapper;
import com.woniuxy.house.mapper.UserMapper;
import com.woniuxy.house.service.SourceService;
import com.woniuxy.house.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SourceServiceImp implements SourceService {
    @Autowired
    private HouseDetailMapper houseDetailMapper;
    @Autowired
    private HousePicturesMapper housePicturesMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SourceMapper sourceMapper;

    @Override
    public List<Source> getByCondition(Source source) {

        return sourceMapper.getSourceListByCondition(source);
    }

    @Override
    public PageBean<Source> getPageByCondition(Source source,Integer page,String sort) {
        PageBean<Source> pageBean = new PageBean<>();
        pageBean.setPageSzie(6);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        //判断是否有排序的筛选条件
        if(sort!=null&&!sort.equals("无")){
            if(sort.equals("价格最低")){
                PageHelper.orderBy("price asc");
            }
            if(sort.equals("时间最新")){
                PageHelper.orderBy("publish_time desc");
            }
            if(sort.equals("距离最近")){


            }
        }

        List<Source> list = sourceMapper.getSourceListByCondition(source);
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }

    @Override
    public void add(Source source, HouseDetail houseDetail, String housePictures[], Integer userId) {
        houseDetail.setSituation("y");
        houseDetail.setContract("n");
        houseDetailMapper.add(houseDetail);
        source.setPublishTime(String.valueOf(LocalDateTime.now()));
        source.setTag("独卫");
        source.setIsrental("n");
        source.setIscross("n");
        source.setUser(userMapper.getById(userId));
        source.setHouseDetail(houseDetail);
        sourceMapper.add(source);
        //将房源图片路径及相应的房源放到数据库
        for (String housePicture : housePictures) {
            HousePictures house=new HousePictures();
           house.setName("1");
          house.setSource(source);
          house.setPicture(housePicture);
            housePicturesMapper.add(house);
        }

    }

    @Override
    public List<Source> getAll() {
        return sourceMapper.getAll();
    }

    @Override
    public void update(Source source) {
       sourceMapper.update(source);
    }

    @Override
    public PageBean<Source> getByPage(int page) {
        //根据条件页码分页大小将PageBean中的属性一次设置
        PageBean<Source> pageBean = new PageBean<>();
        pageBean.setPageSzie(2);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        List<Source> list = sourceMapper.getAll();
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }

    @Override
    public PageBean<Source> getByCondition(Source source, int page) {
        PageBean<Source> pageBean=new PageBean<>();
        pageBean.setPageSzie(6);
        pageBean.setCurrPage(page);
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        List<Source> list = sourceMapper.getSourceListByCondition(source);//根据条件
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(list);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;

    }

    @Override
    public void addUserAndSource(Integer userId, Integer SourceId) {
        sourceMapper.addUserAndSource(userId,SourceId);
    }

    @Override
    public Boolean getUserAndSource(Integer userId, Integer SourceId) {
        if(sourceMapper.getUserAndSource(userId,SourceId)==null){
            return false;
        };
        return true;
    }

    @Override
    public void deleteByUidAndSid(Integer userId, Integer SourceId) {
        sourceMapper.deleteByUidAndSid(userId,SourceId);
    }

    @Override
    public List<Source> getMyConcern(Integer userId) {
    return   sourceMapper.getMyConcern(userId);
    }
}
